package blow.sky.quoted.service;

import blow.sky.quoted.dao.RequestDao;
import blow.sky.quoted.dao.UserInfoDao;
import blow.sky.quoted.entity.ClientViewRequest;
import blow.sky.quoted.entity.QuoteRequest;
import blow.sky.quoted.entity.UserInfo;
import blow.sky.quoted.entity.UserViewRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @ClassName QuoteRequestService
 * @Description TODO
 * @Author 江志武
 * @Date 2021/5/25 10:34
 */
@Service
public class QuoteRequestService {
    @Autowired
    RequestDao requestDao;
    @Autowired
    UserInfoDao infoDao;
    //添加表信息
    public boolean insert(UserViewRequest quoteDemand)
    {
        List<List<String>> json = quoteDemand.getDemand();
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = null;
        try {
            jsonStr = mapper.writeValueAsString(json);
        }
        catch (JsonProcessingException e) {
            e.printStackTrace();
            return false;
        }
        QuoteRequest quoteRequest = new QuoteRequest();
        BeanUtils.copyProperties(quoteDemand, quoteRequest);
        quoteRequest.setDemands(jsonStr);
        requestDao.save(quoteRequest);
        return true;
    }

    //改变表状态
    public boolean updateIsSucceed(long id, boolean result)
    {
        Optional<QuoteRequest> quoteRequest =  requestDao.findById(id);
        if(quoteRequest.isPresent())//当对象不为空时
        {
            QuoteRequest quote = quoteRequest.get();
            quote.setSuccession(result);
            requestDao.save(quote);
            return true;
        }
        else
            return false;
    }

    public List<UserViewRequest> findALLbySuccession(Boolean succession)
    {
        List<QuoteRequest> requests = requestDao.findAllBySuccession(succession);
        List<UserViewRequest> viewRequests = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        for (QuoteRequest request : requests)
        {
            String jsonStr = request.getDemands();
            try {
                List<List<String>> json = mapper.readValue(jsonStr, List.class);
                UserViewRequest viewRequest = new UserViewRequest();
                BeanUtils.copyProperties(request, viewRequest);
                viewRequest.setDemand(json);
                viewRequests.add(viewRequest);
            }
            catch (JsonMappingException e) {
                e.printStackTrace();
            }
            catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return viewRequests;
    }


    //获取所有由发布者发布的未成功信息
    public List<UserViewRequest> findALLbyRequestIdAndFalse(String user)
    {
        List<QuoteRequest> requests = requestDao.findAllByFromUserAndSuccession(user,false);
        List<UserViewRequest> viewRequests = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        for (QuoteRequest request : requests)
        {
            String jsonStr = request.getDemands();
            try {
                List<List<String>> json = mapper.readValue(jsonStr, List.class);
                UserViewRequest viewRequest = new UserViewRequest();
                BeanUtils.copyProperties(request, viewRequest);
                viewRequest.setDemand(json);
                viewRequests.add(viewRequest);
            }
            catch (JsonMappingException e) {
                e.printStackTrace();
            }
            catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return viewRequests;
    }




    //获取所有由发布者发布的报价成功信息
    public List<UserViewRequest> findALLbyRequestIdAndTrue(String user)
    {
        List<QuoteRequest> requests = requestDao.findAllByFromUserAndSuccession(user,true);
        List<UserViewRequest> viewRequests = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        for (QuoteRequest request : requests)
        {
            String jsonStr = request.getDemands();
            try {
                List<List<String>> json = mapper.readValue(jsonStr, List.class);
                UserViewRequest viewRequest = new UserViewRequest();
                BeanUtils.copyProperties(request, viewRequest);
                viewRequest.setDemand(json);
                viewRequests.add(viewRequest);
            }
            catch (JsonMappingException e) {
                e.printStackTrace();
            }
            catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return viewRequests;
    }

    public void deleteAll()
    {
        requestDao.deleteAll();
    }


    //获取所有的未成功报价发布信息
    public List<UserViewRequest> findALL()
    {
        List<QuoteRequest> requests = requestDao.findAllBySuccession(false);
        List<UserViewRequest> viewRequests = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        for (QuoteRequest request : requests)
        {
            String jsonStr = request.getDemands();
            try {
                List<List<String>> json = mapper.readValue(jsonStr, List.class);
                UserViewRequest viewRequest = new UserViewRequest();
                BeanUtils.copyProperties(request, viewRequest);
                viewRequest.setDemand(json);
                viewRequests.add(viewRequest);
            }
            catch (JsonMappingException e) {
                e.printStackTrace();
            }
            catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return viewRequests;
    }


    //获取信息并附加info
    public List<ClientViewRequest> findRequestAndInfo(List<UserViewRequest> userViewRequests)
    {
        List<ClientViewRequest>  clientViewRequests=new ArrayList<>();
        for (UserViewRequest request : userViewRequests)
        {
            UserInfo info=new UserInfo();
            ClientViewRequest clientViewRequest = new ClientViewRequest();
            try{
                info  = infoDao.findByEmail(request.getFromUser());
                BeanUtils.copyProperties(info, clientViewRequest);
                BeanUtils.copyProperties(request, clientViewRequest);
                clientViewRequests.add(clientViewRequest);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }
        return clientViewRequests;
    }
}
